using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace SAInterfaceLibrary
{
    /// <summary>
    /// Returns info about the existing root install.
    /// </summary>
    public class ExistingProjectInfo
    {
        /// <summary>
        /// The location of the release we are looking at.
        /// </summary>
        private string _rootsys;

        /// <summary>
        /// Setup the object with the location of the actual release.
        /// </summary>
        /// <param name="rootsys_dir"></param>
        public ExistingProjectInfo(string rootsys_dir)
        {
            _rootsys = rootsys_dir;
        }

        /// <summary>
        /// Returns the config string at the central 
        /// </summary>
        /// <returns></returns>
        public string ConfigArguments()
        {
            FileInfo fi = new FileInfo(_rootsys + "\\config.status");
            if (!fi.Exists)
            {
                return "";
            }
            using (TextReader reader = fi.OpenText())
            {
                return reader.ReadToEnd();
            }
        }

        /// <summary>
        /// Try to grab the spec from a build that already exists.
        /// </summary>
        /// <returns>The spec that was used for the last build</returns>
        public ProjectBuildConfigSpec GetSpec()
        {
            ///
            /// First, see if we can find the spec for this build
            /// 

            ProjectBuildConfigSpec spec = ProjectBuildConfigSpec.LoadConfigFromFile(_rootsys + @"\" + Configuraiton.Instance.SubdirectoryForScripts + @"\spec.config_spec");
            if (spec == null)
            {
                return null;
            }

            ///
            /// If they setup any sort of special config.status that is, well, perhaps different, then we
            /// should load that as well.
            /// 

            string config_args = ConfigArguments();
            if (config_args.Length > 0)
            {
                spec.ConfigArguments = config_args;
            }
            return spec;
        }

        /// <summary>
        /// Removes old config info.
        /// </summary>
        public void RemoveOldConfigInfo ()
        {
            FileInfo fi = new FileInfo(_rootsys + @"\config\Makefile.config");
            if (fi.Exists)
            {
                fi.Delete();
            }
        }
    }
}
